{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 排序\n",
    "\n",
    "1. 原地排序使用 `sort()`\n",
    "\n",
    "2. 生成新数组使用 `sorted()`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr1= [3, 5, 4, 7, 8, 9, 2]\n",
      "arr_1= [2, 3, 4, 5, 7, 8, 9]\n",
      "arr1= [2, 3, 4, 5, 7, 8, 9]\n",
      "arr2= [[1, 4], [3, 5], [4, 7], [8, 9], [2, 6]]\n",
      "arr_2= [[1, 4], [2, 6], [3, 5], [4, 7], [8, 9]]\n",
      "arr2= [[1, 4], [2, 6], [3, 5], [4, 7], [8, 9]]\n"
     ]
    }
   ],
   "source": [
    "# 一维数组排序\n",
    "arr1 = [3, 5, 4, 7, 8, 9, 2]\n",
    "arr_1 = sorted(arr1)\n",
    "print('arr1=', arr1)\n",
    "print('arr_1=', arr_1)\n",
    "arr1.sort()\n",
    "print('arr1=', arr1)\n",
    "\n",
    "# 二维数组排序\n",
    "arr2 = [[1, 4], [3, 5], [4, 7], [8, 9], [2, 6]]\n",
    "arr_2 = sorted(arr2, key=lambda x: x[0])\n",
    "print('arr2=', arr2)\n",
    "print('arr_2=', arr_2)\n",
    "arr2.sort(key=lambda x: x[0])\n",
    "print('arr2=', arr2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串中字符按照字典序重新排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "acdeqswxz\n"
     ]
    }
   ],
   "source": [
    "s = \"qazwsxedc\"\n",
    "ans = \"\".join(sorted(s))\n",
    "print(ans)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 最大公约数与最小公倍数\n",
    "\n",
    "Python 3.5 及以上版本提供了计算最大公约数的函数 math.gcd\n",
    "\n",
    "Python 3.9 开始 math.lcm 可以直接用于计算最小公倍数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大公约数: 6\n",
      "最小公倍数: 36\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "\n",
    "# 最大公约数 (GCD)\n",
    "a, b = 12, 18\n",
    "x = math.gcd(a, b)\n",
    "print(f\"最大公约数: {x}\")  # 输出: 6\n",
    "\n",
    "# 最小公倍数 (LCM) - 仅支持 Python 3.9+\n",
    "y = math.lcm(a, b)\n",
    "print(f\"最小公倍数: {y}\")  # 输出: 36\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最大公约数: 6\n",
      "最小公倍数: 36\n"
     ]
    }
   ],
   "source": [
    "def gcd(a, b):\n",
    "    while b:\n",
    "        a, b = b, a % b\n",
    "    return a\n",
    "\n",
    "def lcm(a, b):\n",
    "    return abs(a * b) // gcd(a, b)\n",
    "\n",
    "a, b = 12, 18\n",
    "# 最大公约数\n",
    "g = gcd(a, b)\n",
    "print(f\"最大公约数: {g}\")  # 输出: 6\n",
    "# 最小公倍数\n",
    "l = lcm(a, b)\n",
    "print(f\"最小公倍数: {l}\")  # 输出: 36"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
